package com.springboot.nacos.discovery.registry;


import com.alibaba.nacos.api.naming.pojo.Instance;
import small.common.core.util.StringUtil;
import com.small.nacos.api.naming.NamingService;
import com.springboot.nacos.discovery.NacosDiscoveryProperties;
import com.springboot.nacos.discovery.NacosServiceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;

public class NacosServiceRegistry implements ServiceRegistry<Registration> {

    private static final Logger LOGGER = LoggerFactory.getLogger(NacosServiceRegistry.class);

    private final NacosDiscoveryProperties nacosDiscoveryProperties;

    private NacosServiceManager nacosServiceManager;

    public NacosServiceRegistry(NacosServiceManager nacosServiceManager,
                                NacosDiscoveryProperties nacosDiscoveryProperties) {
        this.nacosDiscoveryProperties = nacosDiscoveryProperties;
        this.nacosServiceManager = nacosServiceManager;
    }

    /**
     * 发布事件后会由AbstractAutoServiceRegistration的start方法执行注册
     *
     * @param registration
     */
    @Override
    public void register(Registration registration) {

        if (StringUtil.isEmpty(registration.getServiceId())) {
            LOGGER.warn("No service to register for nacos client...");
            return;
        }
        NamingService namingService = this.namingService();
        Instance instance = getNacosInstanceFromRegistration(registration);
        String serviceId = registration.getServiceId();
        try {
            namingService.registerInstance(serviceId, nacosDiscoveryProperties.getGroup(), instance);
        } catch (Exception e) {
            LOGGER.error("nacos registry, {} register failed...{},", serviceId,
                    registration.toString(), e);
        }
    }

    @Override
    public void deregister(Registration registration) {
        LOGGER.info("De-registering from Nacos Server now...");

        NamingService namingService = this.namingService();
        String serviceId = registration.getServiceId();
        String group = nacosDiscoveryProperties.getGroup();
        Instance instance = getNacosInstanceFromRegistration(registration);
        try {
            namingService.deregisterInstance(serviceId, group, instance);
        } catch (Exception e) {
            LOGGER.error("ERR_NACOS_DEREGISTER, de-register failed...{},",
                    registration.toString(), e);
        }

        LOGGER.info("De-registration finished.");
    }

    @Override
    public void close() {

    }

    @Override
    public void setStatus(Registration registration, String status) {

    }

    @Override
    public <T> T getStatus(Registration registration) {
        return null;
    }

    private Instance getNacosInstanceFromRegistration(Registration registration) {
        Instance instance = new Instance();
        instance.setIp(registration.getHost());
        instance.setPort(registration.getPort());
        instance.setClusterName(nacosDiscoveryProperties.getClusterName());
        instance.setMetadata(registration.getMetadata());
        instance.setEphemeral(nacosDiscoveryProperties.isEphemeral());
        return instance;
    }

    private NamingService namingService() {
        return this.nacosServiceManager.getNamingService(this.nacosDiscoveryProperties);
    }
}
